package com.hanhai.zrb.mobile.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hanhai.common.util.DateUtil;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.UserFollowService;
import com.hanhai.zrb.mobile.result.BaseApiResult;
import com.hanhai.zrb.mobile.util.FrontLoginUtil;
import com.hanhai.zrb.model.common.BaseUser;
import com.hanhai.zrb.model.user.UserFollow;

@Controller
@RequestMapping("userfollow")
@RestController
public class UserFollowController extends BaseController{
	@Resource
	private UserFollowService userFollowService;
	
	@RequestMapping("follow")
	public BaseApiResult follow(@RequestParam Long toUserId) {
		BaseUser user=FrontLoginUtil.getCurrentUser();
		UserFollow userFollow = new UserFollow();
		Long fromUserId = user.getUid();
		userFollow.setFromUserId(fromUserId);
		userFollow.setToUserId(toUserId);
		userFollowService.follow(userFollow);
	
		BaseApiResult result = new BaseApiResult();
		fillRelation(fromUserId, toUserId, result);
		return result;
	}

	private void fillRelation(Long fromUserId, Long toUserId,
			BaseApiResult result) {
		Integer relation=userFollowService.relation(fromUserId, toUserId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("relation", relation);
		result.setData(map);
	}
	
	@RequestMapping("unfollow")
	public BaseApiResult unfollow(@RequestParam Long toUserId) {
		BaseUser user=FrontLoginUtil.getCurrentUser();
		UserFollow userFollow = new UserFollow();
		Long fromUserId = user.getUid();
		userFollow.setFromUserId(fromUserId);
		userFollow.setToUserId(toUserId);
		userFollowService.unfollow(userFollow);
		BaseApiResult result = new BaseApiResult();
		fillRelation(fromUserId, toUserId, result);
		return result;
	}
	
	@RequestMapping("followingListByUpdateTime")
	public BaseApiResult followingListByUpdateTime(String updateTime, Integer limit){
		BaseUser user=FrontLoginUtil.getCurrentUser();
		Date time=DateUtil.parseDateTime(updateTime);
		List<UserFollow> list=userFollowService.followingListByUpdateTime(user.getUid(), time, limit);
		BaseApiResult result = new BaseApiResult();
		result.setData(list);
		return result;
	}

	@RequestMapping("followedListByUpdateTime")
	public BaseApiResult followedListByUpdateTime(String updateTime, Integer limit){
		BaseUser user=FrontLoginUtil.getCurrentUser();
		Date time=DateUtil.parseDateTime(updateTime);
		List<UserFollow> list=userFollowService.followedListByUpdateTime(user.getUid(), time, limit);
		BaseApiResult result = new BaseApiResult();
		result.setData(list);
		return result;
	}
	
	@RequestMapping("relation")
	public BaseApiResult relation(@RequestParam Long fromUserId,@RequestParam Long toUserId){
		BaseApiResult result = new BaseApiResult();
		Integer relation=userFollowService.relation(fromUserId, toUserId);
		result.setData(relation);
		return result;
	}
}
